package com.daikuan.lizh.ui.activity;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jarhead.common.base.BaseActivity;
import com.jarhead.common.commonutils.ToastHelper;
import com.daikuan.lizh.R;
import com.daikuan.lizh.ui.fragment.HomeFragment;
import com.daikuan.lizh.ui.fragment.SecondFragment;
import com.daikuan.lizh.ui.fragment.ThirdFragment;
import com.daikuan.lizh.ui.fragment.UserCenterFragment;

import java.util.ArrayList;

import butterknife.Bind;
import cn.bmob.v3.Bmob;

public class MainActivity extends BaseActivity {


	@Bind(R.id.viewPager)
	ViewPager viewPager;
	@Bind(R.id.tabLayout)
	TabLayout tabLayout;

	BottomTablayoutAdapter adapter;
	private String[] tabTitles = {"首页", "办信用卡", "发现", "我的"};
	private ArrayList<Fragment> fragments = new ArrayList<>();


	@Override
	protected void initToobar() {

	}

	@Override
	public int getLayoutId() {
		return R.layout.activity_main;
	}

	@Override
	public void initPresenter() {
	}

	@Override
	public void initView() {

		fragments.add(new HomeFragment());
		fragments.add(new ThirdFragment());
		fragments.add(new SecondFragment());
		fragments.add(new UserCenterFragment());
		adapter = new BottomTablayoutAdapter(getSupportFragmentManager(), this);

		viewPager.setAdapter(adapter);
		tabLayout.setupWithViewPager(viewPager);
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		for (int i = 0; i < tabLayout.getTabCount(); i++) {
			TabLayout.Tab tab = tabLayout.getTabAt(i);
			if (tab != null) {
				tab.setCustomView(adapter.getTabView(i));
			}
		}

		int index = getIntent().getIntExtra("type", 0);
		tabLayout.getTabAt(index).getCustomView().setSelected(true);
		viewPager.setCurrentItem(index);
	}

	@Override
	public void initData() {
		initBomb();
	}

	/**
	 * 初始化bomb
	 */
	private void initBomb() {
		Bmob.initialize(this, "c2a80cc8aea86225ee13ac505cc2609f");
	}

	class BottomTablayoutAdapter extends FragmentPagerAdapter {

		private Context context;
		private final int TAB_COUNT = 4;
		private ArrayList<Drawable> drawables = new ArrayList<>();

		public BottomTablayoutAdapter(FragmentManager fm, Context context) {
			super(fm);
			this.context = context;
			drawables.add(context.getResources().getDrawable(R.drawable.selector_main_tab_1));
			drawables.add(context.getResources().getDrawable(R.drawable.selector_main_tab_2));
			drawables.add(context.getResources().getDrawable(R.drawable.selector_main_tab_4));
			drawables.add(context.getResources().getDrawable(R.drawable.selector_main_tab_3));
		}

		public View getTabView(int position) {
			View v = LayoutInflater.from(context).inflate(R.layout.custom_tab_view, null);
			TextView tv = (TextView) v.findViewById(R.id.news_title);

			tv.setText(tabTitles[position]);
			ImageView img = (ImageView) v.findViewById(R.id.imageView);
			img.setImageDrawable(drawables.get(position));
			return v;
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}

		@Override
		public int getCount() {
			return TAB_COUNT;
		}
	}

	private long mExitTime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - mExitTime) > 2000) {//
				// 如果两次按键时间间隔大于2000毫秒，则不退出
				ToastHelper.toast("再按一次退出程序");
				mExitTime = System.currentTimeMillis();// 更新mExitTime
			} else {
//                System.exit(0);// 否则退出程序
				finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

}
